<?php 
//this file handles all the jazz for category admin

function addCategory($categoryName, $categoryPosition){
	global $link;
	
	//clean up the incoming vars
	$categoryName = mysql_real_escape_string($categoryName);
	$categoryPosition = mysql_real_escape_string($categoryPosition);
	
	//update the database and push all the categories to make room for the new one
	$result = mysql_query("UPDATE ".TABLE_PREFIX."_categories SET displayOrder = displayOrder + 1 WHERE displayOrder > ".$categoryPosition);
	if(!$result){
		echo "output=mySqlError&error=".mysql_error();
		return;
	}
	
	//insert the new category
	$result = mysql_query("INSERT INTO ".TABLE_PREFIX."_categories (name, displayOrder) 
						   VALUES ('".$categoryName."', ".($categoryPosition + 1).")");
	if(!$result){
		echo "output=mySqlError&error=".mysql_error();
		return;
	}
	
	//report success
	return "output=success&newCategoryID=".mysql_insert_id();
}

function deleteCategory($categoryID, $deleteForums){
	global $link;
	
	//clean up incoming
	$categoryID = mysql_real_escape_string($categoryID);
	$deleteForums = mysql_real_escape_string($deleteForums);
	
	//update the other display orders
	$result = mysql_query("SELECT displayOrder FROM ".TABLE_PREFIX."_categories WHERE categoryID = ".$categoryID);
	$data = mysql_fetch_object($result);
	$displayOrder = $data->displayOrder;
	
	$result = mysql_query("UPDATE ".TABLE_PREFIX."_categories SET displayOrder = displayOrder - 1 WHERE displayOrder > ".$displayOrder);
	if(!$result){
		echo "output=mySqlError&error=".mysql_error();
		return;
	}
	
	//handle the delete
	$result = mysql_query("DELETE FROM ".TABLE_PREFIX."_categories WHERE categoryID = ".$categoryID);
	
	if(!$result){
		echo "output=mySqlError&error=".mysql_error();
		return;
	}
	
	//now if they want to wipe out everything under that category as well, handle that
	if($deleteForums == "true"){
		$result = mysql_query("DELETE f.*, t.*, p.*
							   FROM ".TABLE_PREFIX."_forums f, ".TABLE_PREFIX."_threads t, ".TABLE_PREFIX."_posts p
							   WHERE f.categoryID = ".$categoryID." 
							   AND t.forumID = f.forumID
							   AND p.threadID = t.threadID");
		if(!$result){
			echo "output=mySqlError&error=".mysql_error();
			return;
		}
	}
	
	//we made it!!
	return "output=success";
}
?>